package com.hengxinguotong.hxgtwygsp.utils;

import java.security.MessageDigest;
import java.util.Locale;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

/**
 * MD5和DES加解密的类
 *
 * @version 1.0.0
 * @description:
 * @fileName:Algorithm.java
 * @createTime:2015-9-16 下午2:53:15
 * @author:hjz
 */
public class Algorithm
{

    private static final String UTF8 = "UTF-8";
    private static final String KEY = "xXO00o1=";

    /**
     * MD5加密
     *
     * @param message
     * @return
     * @throws Exception
     */
    public static String Md5Encrypt(String message)
    {
        try
        {
            MessageDigest bmd5 = MessageDigest.getInstance("MD5");
            bmd5.update(message.getBytes());
            StringBuffer buf = new StringBuffer();
            String result = toHexString(bmd5.digest(buf.toString().getBytes(UTF8)));
            return result;
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * DES加密
     *
     * @param message
     * @param key
     * @return
     * @throws Exception
     */
    public static String DesEncrypt(String message, String key)
    {
        try
        {
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(UTF8));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
            IvParameterSpec iv = new IvParameterSpec(key.getBytes(UTF8));
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
            return toHexString(cipher.doFinal(message.getBytes(UTF8)));
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    public static String DesEncrypt(String message)
    {
        try
        {
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            DESKeySpec desKeySpec = new DESKeySpec(KEY.getBytes(UTF8));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
            IvParameterSpec iv = new IvParameterSpec(KEY.getBytes(UTF8));
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
            return toHexString(cipher.doFinal(message.getBytes(UTF8)));
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * DES加密，ECB模式加密
     *
     * @param message
     * @param key
     * @return
     * @throws Exception
     */
    public static String DesEncryptECB(String message, String key)
    {
        try
        {
            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
            DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(UTF8));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            return toHexString(cipher.doFinal(message.getBytes(UTF8)));
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    public static String DesEncryptECB(String message)
    {
        try
        {
            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
            DESKeySpec desKeySpec = new DESKeySpec(KEY.getBytes(UTF8));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            return toHexString(cipher.doFinal(message.getBytes(UTF8)));
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * DES加密，带向量
     *
     * @param message
     * @param key
     * @param mIV
     * @return
     * @throws Exception
     */
    public static String DesEncrypt(String message, String key, String mIV)
    {
        try
        {
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(UTF8));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
            IvParameterSpec iv = new IvParameterSpec(mIV.getBytes(UTF8));
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
            return toHexString(cipher.doFinal(message.getBytes(UTF8)));
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * DES解密
     *
     * @param message
     * @param key
     * @return
     * @throws Exception
     */
    public static String DesDecrypt(String message, String key)
    {
        try
        {
            byte[] bytesrc = convertHexString(message);
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(UTF8));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
            IvParameterSpec iv = new IvParameterSpec(key.getBytes(UTF8));
            cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
            byte[] retByte = cipher.doFinal(bytesrc);
            return new String(retByte);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    public static String DesDecrypt(String message)
    {
        try
        {
            byte[] bytesrc = convertHexString(message);
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            DESKeySpec desKeySpec = new DESKeySpec(KEY.getBytes(UTF8));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
            IvParameterSpec iv = new IvParameterSpec(KEY.getBytes(UTF8));
            cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
            byte[] retByte = cipher.doFinal(bytesrc);
            return new String(retByte);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * DES解密,带向量
     *
     * @param message
     * @param key
     * @param mIV
     * @return
     * @throws Exception
     */
    public static String DesDecrypt(String message, String key, String mIV)
    {
        try
        {
            byte[] bytesrc = convertHexString(message);
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(UTF8));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
            IvParameterSpec iv = new IvParameterSpec(mIV.getBytes(UTF8));
            cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
            byte[] retByte = cipher.doFinal(bytesrc);
            return new String(retByte);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * DES解密，ECB模式解密
     *
     * @param message
     * @param key
     * @return
     * @throws Exception
     */
    public static String DesDecryptECB(String message, String key)
    {
        try
        {
            byte[] bytesrc = convertHexString(message);
            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
            DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(UTF8));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            byte[] retByte = cipher.doFinal(bytesrc);
            return new String(retByte);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    private static byte[] convertHexString(String ss)
    {
        byte digest[] = new byte[ss.length() / 2];
        for (int i = 0; i < digest.length; i++)
        {
            String byteString = ss.substring(2 * i, 2 * i + 2);
            int byteValue = Integer.parseInt(byteString, 16);
            digest[i] = (byte) byteValue;
        }
        return digest;
    }

    private static String toHexString(byte b[])
    {
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < b.length; i++)
        {
            String plainText = Integer.toHexString(0xff & b[i]);
            if (plainText.length() < 2)
                plainText = "0" + plainText;
            hexString.append(plainText);
        }
        return hexString.toString().toUpperCase(Locale.ENGLISH);
    }

}
